package com.ding.easyme.app.util;

import java.io.File;
import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class XMLParse {

	
	public static String getDataFormTag(Element element,String tag,String defaltValue){
		return element.getElementsByTagName(tag).item(0) == null
				|| element.getElementsByTagName(tag).item(0).getFirstChild() == null
				|| element.getElementsByTagName(tag).item(0).getFirstChild()
						.getNodeValue() == null
				|| element.getElementsByTagName(tag).item(0).getFirstChild()
						.getNodeValue().trim().equals("") ? defaltValue
				: element.getElementsByTagName(tag).item(0).getFirstChild()
						.getNodeValue();
	}
	
	public static String getDataFormTag(Document doc,String tag,String defaltValue){
		return doc.getElementsByTagName(tag).item(0) == null 
				|| doc.getElementsByTagName(tag).item(0).getFirstChild() == null
				|| doc.getElementsByTagName(tag).item(0).getFirstChild()
						.getNodeValue() == null
				|| doc.getElementsByTagName(tag).item(0).getFirstChild()
						.getNodeValue().trim().equals("") ? defaltValue
				: doc.getElementsByTagName(tag).item(0).getFirstChild()
						.getNodeValue();
	}
	
	public static String getStringFromNode(Node root) throws IOException {
		StringBuilder result = new StringBuilder();
		if (root.getNodeType() == Node.TEXT_NODE) {
			result.append(root.getNodeValue());
		} else if(root.getNodeType() == Node.CDATA_SECTION_NODE) {
			result.append(addDTDValidation(root.getNodeValue()));
		} else {
			if (root.getNodeType() != Node.DOCUMENT_NODE) {
				result.append("<").append(root.getNodeName()).append(">");
			} else {
				result.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
			}

			NodeList nodes = root.getChildNodes();
			for (int i = 0, j = nodes.getLength(); i < j; i++) {
				Node node = nodes.item(i);
				result.append(getStringFromNode(node));
			}
			if (root.getNodeType() != Node.DOCUMENT_NODE) {
				result.append("</").append(root.getNodeName()).append(">");
			}
		}
		return result.toString();
	}

	public static void createElement(Document doc, Element parentElement, String field, String value,String separator){
		Element element = doc.createElement(field);
		if (value != null)
			element.appendChild(doc.createTextNode(value));
		parentElement.appendChild(doc.createTextNode(separator));
		parentElement.appendChild(element);
	}
	
	public static void createElement(Document doc, Element parentElement, String field, int value,String separator){
		Element element = doc.createElement(field);
		if (value != -1)
			element.appendChild(doc.createTextNode(String.valueOf(value)));
		parentElement.appendChild(doc.createTextNode(separator));
		parentElement.appendChild(element);
	}
	
	
	
	//add by zdg 20110922 
	public static Node getNextSiblingByName(Element key, String name) {
		Node node = key.getNextSibling();
		if(node != null) {
			if(node.getNodeType() == Node.TEXT_NODE) {
				node = node.getNextSibling();
				if(node == null) return null;
			}
			if(name.equals(node.getNodeName())) {
				return node;
			}
		}
		return null;
	}
	
	public static String getStringValue(Element key) {
		Node node = key.getNextSibling();
		if(node != null) {
			if(node.getNodeType() == Node.TEXT_NODE) {
				node = node.getNextSibling();
				if(node == null) return null;
			}
			if("string".equals(node.getNodeName())) {
				return getNodeTextContent(node);
			} 
		}
		return null;
	}
	
	public static String getNodeTextContent(Node node) {
		if(node == null) return null;
		NodeList childs = node.getChildNodes();
		StringBuffer sb = new StringBuffer("");
		if(childs != null) {
			for(int i=0; i<childs.getLength(); i++) {
				Node child = childs.item(i);
				if(child.getNodeType() == Node.TEXT_NODE || child.getNodeType() == Node.CDATA_SECTION_NODE) {
					sb.append(child.getNodeValue());					
				}
			}
		}
		return sb.toString().trim();
	}
	
	public static Document getDocument(File file) throws SAXException,
			IOException, ParserConfigurationException {
		DocumentBuilder builder = DocumentBuilderFactory.newInstance()
				.newDocumentBuilder();
		return builder.parse(file);
	}
	
	public static boolean checkHasElement(NodeList list) {
		if(list == null) return false;
		for(int i=0; i<list.getLength(); i++) {
			Node node = list.item(i);
			if(node.getNodeType() == Node.ELEMENT_NODE) {
				return true;
			}
		}
		return false;
	}
	
	public static String addDTDValidation(String string){
		if(null==string)
			return null;
		else if(string.equals(""))
			return "";
		else 
			return "<![CDATA[" + string + "]]>";
	}
}
